package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {

    public static String pdfToString(InputStream inputStream){
        PDDocument document =null;
        try {
            //加载PDF文档
            document = PDDocument.load(inputStream);
            // 创建一个PDFTextStripper实例来提取文本
            PDFTextStripper pdfStripper = new PDFTextStripper();
            //从PDF文档中提取文本
            String s = pdfStripper.getText(document);
            return s;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                document.close();
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    //测试读取文件内容
    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("D:/AAA/123.pdf");
        String s = pdfToString(fileInputStream);
        System.out.println(s);
    }
}
